home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Speccy ClassiX 1998
/
Speccy ClassiX 98.iso
/
amiga_system
/
the_aminet
/
comm
/
news
/
npns.lha
/
NPNS
/
postnews.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-30
|
7KB
|
307 lines
/*
* Program postnews
* Programmer N.d'Alterio
* Date 06/07/95
*
* Synopsis: This program spools an news article ready for posting at
* some later time. If currently online then sendnews is
* called and the article will be sent off immediately.
* The online check is done by trying to open TCP: daytime
* port.
*
* This is a heavily modified version of postnews by
* James Burton from his postnewsspool package. This fixes
* a number of bugs such as the leaving of locks, hanging
* for a long time while waiting to see if it is online.
* In addition the program has been very much streamlined
* there are no longer the command line options -X, -R as
* these seemd to have no use. Also gone are the arg### and
* ref### files it produced which seemed to be useless.
*
* For tin users the program has the command line option
* -from the add a From: user@somewhere to the header.
*
* This program has the paths for my setup compiled into
* it as defaults to use different paths and files there
* is an env variable PNS_SPOOLDIR which is the full directory
* name for the spool dir with no trailing '/'.
*
* The program expects to have the companion program sendnews
* in AmiTCP:bin/sendnews which in turn needs to have the
* NNTP posting program as AmiTCP:bin/NNTPPost.
*
* Command Line Arguments: -from Adds a From: someone@ line to header
* -h Prints some help info
* filename The article file to post
* if omitted will take from STDIN
*
* Inputs: article Should contain a full header
*
* Outputs: spooldir/msg.### The spooled message
*
* Variables: int i general loop var
* int DoStdin flag
* int exit_code return code for prog
* BPTR lock lock on spooldir
* char *Buffer string to build filenames
* FILE *msgfile file to contain spooled article
* int no_post flag - don't try to send
*
* Functions: copyfile copies article to spooldir
*
* $VER: postnews.c 2.1 (07.08.95) $
* $Log: postnews.c $
* Revision 3.3 1995/09/30 21:25:18 daltern
* new command line option -n
*
* Revision 3.2 1995/09/30 21:17:20 daltern
* Added new env var PNS_OFFLINE
* improved error handling
* ,
*
* Revision 3.1 1995/09/10 16:41:41 daltern
* Removed need for seq file to comply with v3.1 of sendnews
* spooled messages now have name of form msg.##########
* where ###### is the long date returned by time()
*
*
* Revision 1.2 1995/08/27 21:49:10 daltern
* Added env vars for path options
* fixed a bug where a file was left open
*
* Revision 1.1 1995/08/07 14:17:55 daltern
* Initial revision
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dos/dos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <exec/types.h>
#define SPOOLDIR "AmiTCP:Usr/Spool/News"
int copyfile( char *, FILE * );
extern char *version = "$VER: postnews 3.3 (30.09.95) Nick d'Alterio";
extern struct Library *DOSBase;
int main( int argc,char **argv )
{
FILE *msgfile;
int i;
int DoStdin = TRUE;
int exit_code = 0;
int no_post = FALSE;
char *spooldir;
char Buffer[100];
BPTR lock;
FILE *fptr;
/*
* Read spool dir from env variable.
*/
if ( ( spooldir = getenv( "PNS_SPOOLDIR" ) ) == NULL ) {
spooldir = SPOOLDIR;
} /* end if */
/*
* See if user wants to post or not if online.
*/
if ( getenv( "PNS_OFFLINE" ) ) {
no_post = TRUE;
} /* end if */
/*
* Open dos library > v37
*/
if ( OpenLibrary( "dos.library", 37 ) ) {
/*
* Lock directory while using.
*/
if ( lock = Lock( spooldir, ACCESS_READ ) ) {
/*
* Open file to spool articles to.
*/
sprintf( Buffer, "%s/msg.%ld", spooldir, time( NULL ) );
if ( msgfile = fopen( Buffer, "w" ) ) {
/*
* Parse command line.
*/
i = 0;
while ( i < argc ) {
i++;
if ( argv[i][0] == '-' ) {
switch ( argv[i][1] ) {
case 'f' :
if ( strcmp( argv[i], "-from" ) == 0 ) {
fprintf( msgfile, "From: %s\n", argv[i+1] );
} /* end if */
i++;
break;
case 'h' :
fprintf( stderr, " Usage: postnews [-from me@somewhere][-n] article_filename\n" );
fprintf( stderr, "\n Article is taken from stdin if no filename is given\n" );
break;
case 'n' :
no_post = TRUE;
break;
default :
fprintf( stderr, " Unknown option use -h for help\n" );
break;
} /* end switch */
} else {
/*
* Move article file to spool file.
*/
if ( argv[i] != NULL && argv[i][0] != '\0' ) {
exit_code = copyfile( argv[i], msgfile );
DoStdin = FALSE;
} /* end if */
} /* end if */
} /* end while */
/*
* Move article from stdin to spool file.
*/
if ( DoStdin == TRUE ) {
int ch;
while ( ( ch = getchar() ) != EOF ) fputc( ch, msgfile );
} /* end if */
fclose( msgfile );
} else {
fprintf( stderr, " Could not open message file - %s\n", Buffer );
exit_code = 10;
} /* end if open msg file */
UnLock( lock );
} else {
fprintf( stderr, " Could not lock spooldir - %s\n", spooldir );
exit_code = 20;
} /* end if lock */
} else {
fprintf( stderr, " Could not open dos.library > v37\n" );
exit_code = 20;
} /* end if */
/*
* Check if online. If yes then call sendnews to send off articles
* now.
*/
if ( !exit_code ) {
if ( ( ( fptr = fopen( "TCP:localhost/daytime", "r" ) ) == NULL ) || no_post ) {
fprintf( stderr, " You are not online - message spooled\n" );
} else {
fclose( fptr );
fprintf( stderr, " Posting spooled news now....\n" );
system("amitcp:bin/sendnews");
} /* end if */
} /* end if !exit_code */
exit(exit_code);
} /* end program postnews */
/*========================================================================*
END
*========================================================================*/
/*========================================================================*
BEGIN FUNCTION copyfile
*========================================================================*/
int copyfile( char *name, FILE *fp )
{
FILE *infile;
int ch;
infile = fopen( name, "r" );
if ( infile == NULL ) {
fprintf( stderr, " Can't open article to spool\n" );
return 10;
} /* end if */
while ( ( ch = fgetc( infile ) ) != EOF ) fputc( ch, fp );
fclose( infile );
return 0;
} /* end function copyfile */
/*========================================================================*
END copyfile
*========================================================================*/